package com.demo.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.server.annotation.HandleLog;
import com.demo.server.entity.Permission;
import com.demo.server.entity.req.IdReq;
import com.demo.server.entity.req.IntIdReq;
import com.demo.server.entity.req.PermissionIconSetReq;
import com.demo.server.service.PermissionService;
import com.demo.server.util.Result;
import com.demo.server.util.ResultData;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("permission")
@Tag(name= "资源接口")
public class PermissionController {

	@Autowired
	PermissionService permissionService;
	
	@PostMapping("list")
	@ResponseBody
	@Operation(summary = "列出所有资源")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultData<List<Permission>> list(){
		log.info("list>>");
		return permissionService.list();
	}
	
	@PostMapping("listByRoleId")
	@ResponseBody
	@Operation(summary = "列出角色下的资源")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultData<List<Permission>> listByRoleId(@RequestBody IdReq req){
		log.info("listByRoleId>>:{}",req);
		return permissionService.listByRoleId(req.getId());
	}
	
	@PostMapping("listUserPermission")
	@ResponseBody
	@Operation(summary = "列出当前用户资源")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultData<List<Permission>> listUserPermission(){
		log.info("listUserPermission>>");
		return permissionService.listUserPermission();
	}
	
	@PostMapping("listUserMenu")
	@ResponseBody
	@Operation(summary = "列出当前用户菜单")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public ResultData<List<Permission>> listUserMenu(){
		log.info("listUserMenu>>");
		return permissionService.listUserMenu();
	}
	
	@HandleLog
	@PostMapping("add")
	@ResponseBody
	@Operation(summary = "添加资源")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result add(@RequestBody Permission req){
		log.info("addPermission>>:{}",req);
		return permissionService.add(req);
	}
	
	@HandleLog
	@PostMapping("edit")
	@ResponseBody
	@Operation(summary = "修改资源")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result edit(@RequestBody Permission req){
		log.info("edit>>:{}",req);
		return permissionService.edit(req);
	}
	
	@HandleLog
	@PostMapping("delete")
	@ResponseBody
	@Operation(summary = "删除资源")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result delete(@RequestBody IntIdReq req){
		log.info("delete>>:{}",req);
		return permissionService.delete(req.getId());
	}
	
	@HandleLog
	@PostMapping("resetIcon")
	@ResponseBody
	@Operation(summary = "重置图标")
	@Parameter(name="token",description = "token",required = true,in = ParameterIn.HEADER)
	public Result resetIcon(@RequestBody List<PermissionIconSetReq> req){
		log.info("resetIcon>>:{}",req);
		return permissionService.resetIcon(req);
	}
}
